package cn.jingzhuan.fm.ui.epoxy.controller

import android.content.Context
import cn.jingzhuan.fm.DividerFmBindingModel_
import cn.jingzhuan.fm.ItemFmAlbumRecommendBindingModel_
import cn.jingzhuan.fm.ViewFmModuleTitleBindingModel_
import cn.jingzhuan.fm.pojo.ADBanner
import cn.jingzhuan.fm.pojo.FMAlbum
import cn.jingzhuan.fm.pojo.AlbumTodayListen
import cn.jingzhuan.fm.ui.activity.FMAlbumDetailActivity
import cn.jingzhuan.fm.ui.epoxy.model.FMIndexBannerModel_
import cn.jingzhuan.fm.ui.epoxy.model.FMIndexTodayListenModel_
import com.airbnb.epoxy.AutoModel
import com.airbnb.epoxy.EpoxyController

class FMIndexAdapterController(var context: Context) : EpoxyController() {

    var id : Int = 0
    private var mBannerList = mutableListOf<ADBanner>()
    private var mTodayListenList = mutableListOf<AlbumTodayListen>()
    private var mAlbumRecommendList = mutableListOf<FMAlbum>()

    @AutoModel
    lateinit var fmIndexBannerModel : FMIndexBannerModel_
    @AutoModel
    lateinit var fmIndexTodayListenModel : FMIndexTodayListenModel_

    override fun buildModels() {
        addBannerView()
        addTitleView("今日必听")
        addTodayListenView()
        addTitleView("专辑推荐")
        addAlbumRecomendView()
    }

    private fun addAlbumRecomendView() {
        if (mAlbumRecommendList.isNotEmpty()) {
            var first = true
            mAlbumRecommendList.forEach {
                if (!first) {
                    DividerFmBindingModel_()
                            .id(id++)
                            .addTo(this)
                }
                ItemFmAlbumRecommendBindingModel_()
                        .id(id++)
                        .album(it)
                        .clickListener( {v ->
                             FMAlbumDetailActivity.startActivity(v.context, it)
                        })
                        .addTo(this)
                first = false
            }
        }
    }

    private fun addTodayListenView() {
        if (mTodayListenList.isNotEmpty()) {
            fmIndexTodayListenModel.todayListenList = mTodayListenList
            fmIndexTodayListenModel.addTo(this)
        }
    }

    private fun addTitleView(title: String) {
        ViewFmModuleTitleBindingModel_()
                .id(id++)
                .title(title)
                .addTo(this)
    }

    private fun addBannerView() {
        if (mBannerList.isNotEmpty()) {
            fmIndexBannerModel.mBannerList = mBannerList
            fmIndexBannerModel.addTo(this)
        }
    }

    fun setBannerList(bannerList: List<ADBanner>) {
        if(bannerList.isNotEmpty()) {
            this.mBannerList.clear()
            this.mBannerList.addAll(bannerList)
            requestModelBuild()
        }
    }

    fun setTodayListenList(todayListenList: List<AlbumTodayListen>) {
        if(todayListenList.isNotEmpty()) {
            this.mTodayListenList.clear()
            this.mTodayListenList.addAll(todayListenList)
            requestModelBuild()
        }
    }

    fun setAlbumRecommendList(albumRecommendList: List<FMAlbum>) {
        if (albumRecommendList.isNotEmpty()) {
            this.mAlbumRecommendList.clear()
            this.mAlbumRecommendList.addAll(albumRecommendList)
            requestModelBuild()
        }
    }

}